﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeneticAlgorythm.Algorythm;
using System.Diagnostics;

namespace GeneticAlgorythm.Problem
{
    public class ObjectiveFunctionCalculatorCPU : ObjectiveFunctionValueCalculatotBase
    {
        private long[] _timerValues = new long[2];
        private Stopwatch _timer = new Stopwatch();


        public override float calculate(Chromosome chromosome, float[,] cargoQuantityMatrix, float[,] costsMatrix)
        {
            float result = 0;
            int size = chromosome.getSize();
            for (int j = 0; j < size; ++j)
            {
                for (int k = 0; k < size; ++k)
                {
                    if (j == k) continue;
                    result += cargoQuantityMatrix[j, k] * costsMatrix[(int)chromosome.getGene(j) - 1, (int)chromosome.getGene(k) - 1]; ;
                }
            }
            return result;
        }
        public override float[] calculate(List<Chromosome> chromosomes, float[,] cargoQuantityMatrix, float[,] costsMatrix)
        {
            //_timer.Restart();
            //int chromosomeSize = chromosomes[0].getSize();
            //int numberOfChromosomes = chromosomes.Count;

            //float[] preparedCargoQuantityVector = prepareCargoQuantityVector(cargoQuantityMatrix);
            //float[,] preparedCostsMatrices = prepareCostsMatrices(chromosomes, costsMatrix);

            //_timer.Stop();
            //_timerValues[0] = _timer.ElapsedMilliseconds;
            //_timer.Restart();
            
            //float[] result = new float[numberOfChromosomes];

            //for (int i = 0; i < numberOfChromosomes; i++)
            //{
            //    for (int j = 0; j < chromosomeSize * chromosomeSize; j++)
            //    {
            //         result[i] += preparedCostsMatrices[j, i] * preparedCargoQuantityVector[j];
            //    }
                
            //}
            //_timer.Stop();
            //_timerValues[1] = _timer.ElapsedMilliseconds;
            //return result;

            _timer.Restart();
            int numberOfChromosomes = chromosomes.Count;
            float[] result = new float[numberOfChromosomes];
            for(int i = 0 ; i < numberOfChromosomes; i++)
            {
                result[i] = calculate(chromosomes[i], cargoQuantityMatrix, costsMatrix);
            }
            _timer.Stop();

            _timerValues[0] = _timer.ElapsedMilliseconds;
            _timerValues[1] = _timer.ElapsedMilliseconds;
            return result;
        }

        public override long[] getTimerValues()
        {
            return _timerValues;
        }
    }
}
